#ifndef __GL__RENDERER__H__
#define __GL__RENDERER__H__

#include "../PicFrame.h"
#include "GLCommon.h"

#ifdef __ANDROID__
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <GLES2/gl2platform.h>
#endif

namespace simple_renderer {

#define GLES2_MAX_PLANE 3

class GLRenderer {
public:
    GLRenderer();
    virtual ~GLRenderer();

    int InitRenderer();
    void DrawPicFrame(const PicFrame &pic_frame, int win_w, int win_h);
    int ReleaseRenderer();
    void ClearWindow(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha);

protected:
    virtual int InitBase(const char* fragment_shader_str);
    virtual int ReleaseBase();

    virtual const char *GetFragmentShader() = 0;
    virtual int InitExtend() = 0;
    virtual int UseProgram() = 0;
    virtual int UploadTexture(const PicFrame &pic_frame) = 0;

    int DrawFrame(const PicFrame &pic_frame, int win_w, int win_h);

    virtual void ResetTexCoords();
    virtual void ReloadTexCoords();

    virtual void ResetVertices();
    virtual void ReloadVertices();

    virtual void GLES2_loadOrtho(GLES_Matrix *matrix, GLfloat left, GLfloat right, GLfloat bottom, GLfloat top, GLfloat near, GLfloat far);

protected:

    GLuint program;

    GLuint vertex_shader;
    GLuint fragment_shader;
    GLuint plane_textures[GLES2_MAX_PLANE];

    GLuint av4_position;
    GLuint av2_texcoord;
    GLuint um4_mvp;

    GLuint us2_sampler[GLES2_MAX_PLANE];
    GLuint um3_color_conversion;

    GLsizei buffer_width;
    GLsizei visible_width;

    GLfloat texcoords[8];

    GLfloat vertices[8];
    int     vertices_changed;

    int     format;
    int     gravity;
    GLsizei layer_width;
    GLsizei layer_height;
    int     frame_width;
    int     frame_height;
    int     frame_sar_num;
    int     frame_sar_den;

    bool _renderer_inited;
    std::mutex _init_mux;
};

}

#endif